//Occupational Sorting
use LopNr_PersonNr year Kon KU1Ssyk4 year Kommun Sun2000niva  Fodelsear using "[Folder]\Panel_Ind.dta" , clear


merge m:1 LopNr_PersonNr using "[Folder]\TimeInv_Ind.dta", keepusing(FodelseLandnamn InvAr FodelseLandnamnFar FodelseLandnamnMor)
drop if _merge==2
drop _merge
drop if missing(FodelseLandnamn)

//Adding other source of parental CoB
merge m:1 LopNr_PersonNr using "[Folder]\ParRegs_Data.dta", keepusing( FodelseLandnamn_Far FodelseLandnamn_Mor)
drop if _merge==2
drop _merge
replace FodelseLandnamnFar = FodelseLandnamn_Far if missing(FodelseLandnamnFar)
replace FodelseLandnamnMor = FodelseLandnamn_Mor if missing(FodelseLandnamnMor)


global varadds  WVInd Y003 loggdp05 

rename FodelseLandnamn FodelseLandnamn_Org
rename FodelseLandnamnFar FodelseLandnamn

//Add characteristics for father's CoB
merge m:1 FodelseLandnamn using "[Data Folder]\MacroData_ToMona_v1", keepusing($varadds)
drop if _merge==2
drop _merge


foreach x of var $varadds { 
	rename `x' `x'_CF 
} 


rename FodelseLandnamn FodelseLandnamnFar


//Add characteristics for mother's CoB
rename FodelseLandnamnMor FodelseLandnamn

merge m:1 FodelseLandnamn using "[Data Folder]\MacroData_ToMona_v1", keepusing($varadds)
drop if _merge==2
drop _merge


foreach x of var $varadds { 
	rename `x' `x'_CM 
} 

rename FodelseLandnamn FodelseLandnamnMor
rename FodelseLandnamn_Org FodelseLandnamn


//Add characteristics for own CoB
merge m:1 FodelseLandnamn using "[Data Folder]\MacroData_ToMona_v1", keepusing($varadds)
drop if _merge==2
drop _merge


save  "[Folder]\OccSort_Data.dta", replace

//Add occupation data
drop if year <2014

merge m:1 KU1Ssyk4 using  "[Data Folder]\ssyk12_isco08_up.dta"  , keepusing(iti  nr_ca nr_ci rc rm nr_mp)
drop if _merge==2
drop _merge
save  "[Folder]\OccSort_Data_2014.dta", replace

clear 
use "[Folder]\OccSort_Data.dta"

drop if year==2014
merge m:1 KU1Ssyk4 using  "[Data Folder]\ssyk96_isco08_up.dta"  , keepusing(iti nr_ca nr_ci rc rm nr_mp)
drop if _merge==2
drop _merge

append using  "[Folder]\OccSort_Data_2014.dta"

save  "[Folder]\OccSort_Data.dta", replace


use  "[Folder]\OccSort_Data.dta", clear

drop if Kon ==2
gen Age=year-Fodelsear
drop if Age>60 ///Keep working-age population
drop if Age<24
gen nr = nr_mp+nr_ca -rm-rc
xtile pc_nr=nr, nq(100)


global varadds  WVInd Y003 loggdp05 

//Generate parental CoB averages
foreach x of var $varadds { 
	egen `x'_P = rowmean(`x'_CF `x'_CM ) 
	replace `x'_P = 0 if  (FodelseLandnamnMor==107 | FodelseLandnamnMor==. ) & (FodelseLandnamnFar==107 | FodelseLandnamnFar==. )
} 

egen ClustGroup = group(FodelseLandnamnFar FodelseLandnamnMor)


//Generate variables
// Years of Education.
nsplit Sun2000niva, digits(1 1 1) generate(SunD1 SunD2 SunD3)
gen Educ_Ar = 0
replace Educ_Ar = 3 if SunD1==9
replace Educ_Ar = 6 if SunD1==1
replace Educ_Ar = 9 if SunD1==2
replace Educ_Ar = 10 if SunD1==3 & SunD2==1
replace Educ_Ar = 11 if SunD1==3 & SunD2==2
replace Educ_Ar = 12 if SunD1==3 & SunD2==3
replace Educ_Ar = 13 if SunD1==4 & SunD2==1
replace Educ_Ar = 14 if SunD1==5 & SunD2==2
replace Educ_Ar = 15 if SunD1==5 & SunD2==3
replace Educ_Ar = 16 if SunD1==5 & SunD2==4
replace Educ_Ar = 17 if SunD1==5 & SunD2==5
replace Educ_Ar = 18 if SunD1==6 & SunD2==0
replace Educ_Ar = 18 if SunD1==6 & SunD2==2
replace Educ_Ar = 20 if SunD1==6 & SunD2==4

drop SunD1 SunD2 SunD3

//Experience
gen Exp=Age - Educ_Ar
gen Exp_Sq=Exp^2

//Time in Sweden vars
gen ChildMig = 0
replace ChildMig = 1 if InvAr - Fodelsear < 17 & FodelseLandnamn!=107

gen SweTime=year-InvAr
replace SweTime=0 if FodelseLandnamn==107 
replace SweTime=0 if SweTime<0 //Time of year effects for same mig. year

gen SweTime_Sq = SweTime^2

gen For=0
replace For=1 if FodelseLandnamn!=107

replace Y003=0 if For==0
replace loggdp05=0 if For==0
 
gen For_Sec =0
replace For_Sec=1 if FodelseLandnamn==107 & ( (FodelseLandnamnMor!=107 & FodelseLandnamnMor!=. ) | (FodelseLandnamnFar!=107 & FodelseLandnamnFar!=. ))



cd "[Output Folder]\"


eststo  clear
eststo: reg pc_nr i.year i.Kommun  Educ_Ar Exp Exp_Sq  SweTime SweTime_Sq For Y003 if  ChildMig==0  & Age>24 & Age<60  , vce(cluster FodelseLandnamn)
eststo: reg pc_nr i.year i.Kommun  Educ_Ar Exp Exp_Sq  SweTime SweTime_Sq  For Y003 iti if  ChildMig==0  & Age>24 & Age<60  , vce(cluster FodelseLandnamn)
eststo: reg pc_nr i.year i.Kommun  Educ_Ar Exp Exp_Sq  SweTime SweTime_Sq  For Y003 iti loggdp05   if  ChildMig==0  & Age>24 & Age<60 , vce(cluster FodelseLandnamn)

esttab using OccSortFirst_GDP.csv, se ar2 compress keep(Educ_Ar Exp Exp_Sq For Y003 loggdp05 iti ) star(* 0.10 ** 0.05 *** 0.01)  replace
 
 
 
 
eststo  clear
eststo: reg pc_nr i.year i.Kommun  Educ_Ar Exp Exp_Sq  For_Sec Y003_P if For==0 & Age>24 & Age<60  , vce(cluster ClustGroup)
eststo: reg pc_nr i.year i.Kommun  Educ_Ar Exp Exp_Sq  For_Sec Y003_P iti if For==0 & Age>24 & Age<60   , vce(cluster ClustGroup)
eststo: reg pc_nr i.year i.Kommun  Educ_Ar Exp Exp_Sq  For_Sec Y003_P loggdp05_P iti  if For==0 &  Age>24 & Age<60 , vce(cluster ClustGroup)

esttab using OccSortSecGen_GDP.csv, se ar2 compress keep(Educ_Ar      Exp Exp_Sq For_Sec Y003_P    loggdp05_P iti   ) star(* 0.10 ** 0.05 *** 0.01) replace


